Imports System.Data.SqlClient
Public Class cls_moneda_

    Implements ICloneable

#Region "Variables ..."
    Private int_idmoneda As Integer = 0
    Private str_nombre As String = ""
    Private str_descripcion As String = ""
    Private str_simbolo As String = ""
    Private int_idregistro As Integer = 0

    Private obj_registro As cls_registro_
    Private obj_cotizacion_ As cls_cotizacion_

    Private col_Items As Collections.Generic.List(Of cls_moneda_) = Nothing
#End Region

    Public Overrides Function ToString() As String
        Select Case str_formatoCombos
            Case "Nombre#########(15) - sigla(5)"
                Return nombre.PadRight(15) & " - " & simbolo.PadRight(5)
            Case "sigla(5) - Nombre#########(15)"
                Return simbolo.PadRight(5) & " - " & nombre.PadRight(15)
            Case "Nombre - sigla"
                Return nombre & " - " & simbolo
            Case "sigla - Nombre"
                Return simbolo & " - " & nombre
            Case Else
                Return simbolo & " - " & nombre
        End Select
    End Function

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idmoneda
        End Get
    End Property
    Property idmoneda() As Integer
        Get
            idmoneda = int_idmoneda
        End Get
        Set(ByVal value As Integer)
            int_idmoneda = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property descripcion() As String
        Get
            descripcion = str_descripcion
        End Get
        Set(ByVal value As String)
            str_descripcion = value
        End Set
    End Property
    Property simbolo() As String
        Get
            simbolo = str_simbolo
        End Get
        Set(ByVal value As String)
            str_simbolo = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property cotizacion_() As cls_cotizacion_
        Get
            If IsNothing(obj_cotizacion_) Then
                obj_cotizacion_ = New cls_cotizacion_
                If Not obj_cotizacion_.CargarXidmoneda(idmoneda) Then
                    obj_cotizacion_ = Nothing
                End If
            End If
            cotizacion_ = obj_cotizacion_
        End Get
        Set(ByVal value As cls_cotizacion_)
            obj_cotizacion_ = value
        End Set
    End Property
    ReadOnly Property ultimacotizacion() As cls_cotizacion_
        Get
            ultimacotizacion = New cls_cotizacion_
            If Not ultimacotizacion.BuscarUltimaXidmoneda(idmoneda) Then
                ultimacotizacion = Nothing
            End If
            'ultimacotizacion = ultimacotizacion
        End Get
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_moneda_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_moneda_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_moneda_)
        While obj_Reader.Read()
            Dim Elemento As New cls_moneda_
            Elemento.int_idmoneda = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion"))) = False Then
                Elemento.str_descripcion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo"))) = False Then
                Elemento.str_simbolo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idmoneda = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idmoneda")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion"))) = False Then
            str_descripcion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo"))) = False Then
            str_simbolo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("simbolo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Nuevo = False
        Dim obj_Conexion As New cls_Conexion
        Try

            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__Nuevo"
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@descripcion", SqlDbType.NVarChar).Value = str_descripcion
                .Parameters.Add("@simbolo", SqlDbType.NVarChar).Value = str_simbolo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
            Nuevo = True
            CargarUltimo()

            'Creacion de Cotizacion--------------------------------------------------
            If Not IsNothing(cotizacion_) Then
                If Not IsNothing(cotizacion_.Items) Then
                    For Each Cotizacion As cls_cotizacion_ In cotizacion_.Items
                        Cotizacion.idmoneda = idmoneda
                        Cotizacion.Nuevo(True)
                    Next
                End If
            End If
            '-------------------------------------------------------------------------

        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try

            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__Modificar"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = int_idmoneda
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@descripcion", SqlDbType.NVarChar).Value = str_descripcion
                .Parameters.Add("@simbolo", SqlDbType.NVarChar).Value = str_simbolo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With

            'Creacion/modificacion de Cotizacion-------------------------------------
            If Not IsNothing(cotizacion_) Then
                If Not IsNothing(cotizacion_.Items) Then
                    For Each Cotizacion As cls_cotizacion_ In cotizacion_.Items
                        If Cotizacion.idcotizacion = 0 Then
                            Cotizacion.idmoneda = idmoneda
                            Cotizacion.Nuevo(True)
                        Else
                            Cotizacion.Modificar()
                        End If
                    Next
                End If
            End If
            '-------------------------------------------------------------------------
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idmoneda As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__Eliminar"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = idmoneda
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idmoneda As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__Buscar"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = idmoneda
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function MonedaLocal() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            MonedaLocal = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__Buscar"
                .Parameters.Add("@idmoneda", SqlDbType.Int).Value = 1
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                MonedaLocal = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar(MonedaLocal) el registro de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "moneda__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de moneda_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_moneda_Clon As New cls_moneda_
        obj_moneda_Clon.int_idmoneda = Me.int_idmoneda
        obj_moneda_Clon.str_nombre = Me.str_nombre
        obj_moneda_Clon.str_descripcion = Me.str_descripcion
        obj_moneda_Clon.str_simbolo = Me.str_simbolo
        obj_moneda_Clon.int_idregistro = Me.int_idregistro
        obj_moneda_Clon.col_Items = Me.col_Items
        Return obj_moneda_Clon
    End Function

    Public Function cotizar(ByRef prm_valor As Decimal, Optional prm_moneda As cls_moneda_ = Nothing) As Decimal
        If Not IsNothing(ultimacotizacion) Then
            If IsNothing(prm_moneda) Then
                Return Math.Round(prm_valor / ultimacotizacion.valor, int_cantdecimales)
            Else
                Dim tmp As Decimal = prm_valor * prm_moneda.ultimacotizacion.valor
                Return Math.Round(tmp / ultimacotizacion.valor, int_cantdecimales)
            End If
        End If
    End Function
End Class
